﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Highchart.Core.Data.Chart;
using Highchart.Core;

using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace Highchart.Web
{
    public partial class JSONtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var series = new SerieCollection();

            Random _rnd = new Random();

            PointCollection _pc = new PointCollection();
            _pc.Add(new Point(_rnd.Next(1000)));
            series.Add(new Serie { id = "Serie1", data = _pc.ToArray() });

            _pc = new PointCollection();
            _pc.Add(new Point(_rnd.Next(1000)));
            series.Add(new Serie { id = "Serie2", data = _pc.ToArray() });

            // .: The ToString() function returns a value that starts with 'series: ' which makes it invalid JSON
            // .: Fix that by either returning the substring of series.ToString() starting after the eighth position, OR 
            //    by adding a reference to the Newtonsoft assembly from Highchart project and then serializing the series as done in the commented line below
            //Response.Write(JsonConvert.SerializeObject(series, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
            Response.Write(series.ToString().Substring(8)); 
        }
    }
}